package br.com.portalecbahia;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import br.com.portalecbahia.connectionservices.SynchronizeNotification;
import br.com.portalecbahia.connectionservices.SynchronizeTwitter;
import br.com.portalecbahia.dao.NotificationDAO;
import br.com.portalecbahia.listner.NotificationListeners;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService implements NotificationListeners{
  public static String id;

  public GCMIntentService() {
    super(SplashActivity.SENDER_ID);
  }
  
  @Override
  protected void onRegistered(Context ctxt, String regId) {
    Log.d(getClass().getSimpleName(), "onRegistered: " + regId);
    id = regId;
    
    
	boolean enable = new NotificationDAO(this).findStatusNotification();
	
	if(enable){
		 new SynchronizeNotification(this).execute(true);
	}else{
		 new SynchronizeNotification(this).execute(false);

	}
  }

  @Override
  protected void onUnregistered(Context ctxt, String regId) {
    Log.d(getClass().getSimpleName(), "onUnregistered: " + regId);
  }

  @Override
  protected void onMessage(Context ctxt, Intent message) {
	  boolean enable = new NotificationDAO(this).findStatusNotification();
	  if(enable){
		  String title = (String)  message.getExtras().getString("title")   ;
	
		  String messageByServer = (String)  message.getExtras().getString("message")   ;
	      Log.d(getClass().getSimpleName(), messageByServer);
	      
		   // Prepare intent which is triggered if the
		   // notification is selected
		
		   Intent intent = new Intent(this, MainActivity.class);
		   intent.addCategory(Intent.CATEGORY_LAUNCHER);
		   intent.setAction(Intent.ACTION_MAIN);
			intent.putExtra("openMatches", true);

		   PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
		
		   // Build notification
		   // Actions are just fake
		   Notification noti = new Notification.Builder(this)
		           .setContentTitle(title)
		           .setContentText(messageByServer)
		           .setSmallIcon(R.drawable.game_gol)
		           .setContentIntent(pIntent).build();
		       
		     
		   NotificationManager notificationManager = 
		     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		   // Hide the notification after its selected
		   noti.flags |= Notification.FLAG_AUTO_CANCEL;
		   noti.defaults|=Notification.DEFAULT_VIBRATE;
		   noti.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://" + getPackageName() + "/raw/notificationsound");
	
		   notificationManager.notify(0, noti); 
	  }
   
  }

 
  @Override
  protected void onError(Context ctxt, String errorMsg) {
    Log.d(getClass().getSimpleName(), "onError: " + errorMsg);
  }

  @Override
  protected boolean onRecoverableError(Context ctxt, String errorMsg) {
    Log.d(getClass().getSimpleName(), "onRecoverableError: " + errorMsg);
    
    return(true);
  }

	@Override
	public void onSynchronizeNotificationComplete(Boolean response) {
		
	}
}